
import { locGet, locSet } from '@/utils/common'
import { InjectionKey } from 'vue'
import { createStore, Store, useStore as baseUseStore } from 'vuex'

import { product } from './modules/product'
import type { RootState, AllStateTypes, BreadcrumbItem } from './types/index'

export const key: InjectionKey<Store<RootState>> = Symbol('store')

export const store = createStore<RootState>({
  state: {
    locale: locGet('locale') || 'zhCN',
    // identity: ['admin', 'visitor', 'vip'], // 身份权限，和指令v-permission联合使用
    identity: ['visitor'], // 身份权限，和指令v-permission联合使用
    isExpandNav: false,
    breadcrumb: locGet('breadcrumb', 'O') || [],
    keepAlive: false
  },
  mutations: {
    setLocale (state: { locale: string }, data: string) {
      state.locale = data
      localStorage.setItem('locale', data)
      locSet('locale', data)
    },
    changeNavStatus (state: RootState, data:Boolean) {
      state.isExpandNav = data
    },
    addBreadcrumb (state: RootState, data:BreadcrumbItem) {
      state.breadcrumb.push(data)
      locSet('breadcrumb', state.breadcrumb)
    },
    setBreadcrumb (state: RootState, data:BreadcrumbItem[]) {
      state.breadcrumb = data
      locSet('breadcrumb', state.breadcrumb)
    },
    delBreadcrumb (state: RootState, i:number) {
      state.breadcrumb.splice(i, 1)
      locSet('breadcrumb', state.breadcrumb)
    },
    setKeepAlive (state: RootState, data:Boolean) {
      state.keepAlive = data
    }
  },
  actions: {
    setLocale ({ commit }: any, data: string) {
      commit('setLocale', data)
    },
    changeNavStatus ({ commit }: any, data:Boolean) {
      commit('changeNavStatus', data)
    },
    addBreadcrumb ({ commit }: any, data:Boolean) {
      commit('addBreadcrumb', data)
    },
    delBreadcrumb ({ commit }: any, i:string) {
      commit('delBreadcrumb', i)
    }
  },
  getters: {

  },
  modules: {
    product
  }
})

// 定义自己的 `useStore` 组合式函数
export function useStore<T = AllStateTypes> () {
  return baseUseStore<T>(key)
}
